﻿namespace ConsoleApp.Gof.MapFunction
{
    public class QueryGrantTypeService
    {
        private GrantTypeSerive grantTypeSerive = new();
        private Dictionary<string, Func<string, string>> grantTypeDic = new();

        public QueryGrantTypeService()
        {
            dispatcherInit();
        }

        /**
         *  初始化业务分派逻辑,代替了if-else部分
         *  key: 优惠券类型
         *  value: lambda表达式,最终会获得该优惠券的发放方式
         */

        private void dispatcherInit()
        {
            grantTypeDic.Add("红包", resourceId => grantTypeSerive.redPaper(resourceId));
            grantTypeDic.Add("购物券", resourceId => grantTypeSerive.shopping(resourceId));
            grantTypeDic.Add("qq会员", resourceId => grantTypeSerive.QQVip(resourceId));
        }

        public string getResult(string resourceType)
        {
            string resourceId = "2";
            Func<string, string> typFunc = grantTypeDic[resourceType];
            //传入resourceId 执行这段表达式获得String型的grantType
            var result = typFunc.Invoke(resourceId);
            return result;
        }
    }
}